package com.example.dameng.controller;

import com.example.dameng.api.CommonResult;
import com.example.dameng.entity.*;
import com.example.dameng.service.SubService;
import com.example.dameng.utils.MonthList;
import com.sun.media.jfxmedia.events.VideoTrackSizeListener;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@ResponseBody
@RestController
public class SubstationController {
    @Autowired
    SubService subService;

    //街道信息
    @RequestMapping(value = "/sub/getStreet")
    public CommonResult getStreet(@RequestParam int adminCode){
        List<T_community> array = subService.getStreet(adminCode);
        return CommonResult.success(array);
    }

    //各街道总人数列表
    @RequestMapping(value = "/sub/StreetList")
    public CommonResult StreetList(@RequestParam int adminCode){
        List<StreetList> list = subService.getStreetList(adminCode);
        return CommonResult.success(list);
    }

    @RequestMapping(value = "/sub/apply")
    public CommonResult apply(@RequestBody T_supplystatus supplyStatus) throws ParseException {
        System.out.println("---供电申请测试---");
        System.out.println(supplyStatus);
        //计算前一天日期并返回"yyyy-MM-dd"类型字符串
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String str=supplyStatus.getLogDate();
        Date dt=sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_YEAR,-1);//日期减1天
        Date dt1=rightNow.getTime();
        String yesterday = sdf.format(dt1);
        //计算得到时间
        System.out.println(yesterday);
        Double supplyNum = subService.getSupplyNum(yesterday,supplyStatus.getCommunityCode());
        //前一天的供电量
        System.out.println(supplyNum);
        supplyStatus.setSupplyNum(1.2*supplyNum);
        //当前供电量
        System.out.println(supplyStatus.getSupplyNum());
        //生成申请时间
        supplyStatus.setSupplyTime(new Timestamp(System.currentTimeMillis()));
        subService.setSupplyStatus(supplyStatus);
        return CommonResult.success("");
    }

    @RequestMapping(value = "/sub/report")
    public CommonResult report(@RequestParam String startMonth,@RequestParam String endMonth,@RequestParam String communityName){
        System.out.println("---报表测试---");
        System.out.println(communityName);
        List<String> list = MonthList.getMonthBetweenDate(startMonth,endMonth);
        List<SubReport> reportList = new ArrayList<>();
        System.out.println(list);
        for(String s:list){
            System.out.println(s);
            reportList.add(subService.getMonthReport(s,communityName));
        }
        return CommonResult.success(reportList);
    }
}
